// JavaScript source code
var http = require("http"),
path = require("path"),
fs = require("fs"),
    mime = require("mime");

var context = require("./context.js");
var router = require("./router.js");

http.createServer((req, res) => {

    context(req, res);
    router(req, res);

    req.url = req.url.toLowerCase(), req.method = req.method.toLowerCase();
    if (req.url === "/" || req.url === "/index" && req.method === "get") {
        fs.readFile(path.join(__dirname, "view", "index.html"), (err, data) => {
            if (err) {
                throw err;
            } else {

                res.end(data);
            }
        });
    } else if (req.url === "/submit" && req.method === "get") {
        console.log(1);
    } else if (req.url === "/item" && req.method === "get") {
        console.log(1);
    } else if (req.url === "/add" && req.method === "get") {
        console.log(1);
    } else if (req.url === "/add" && req.method === "post") {
        console.log(1);
    } else {
        res.writeHead(404, "not found", {
            "Content-Type": "text/html;charset=utf-8"
        });
    }
    res.end("404,not found page")



    var publicDir = path.join(__dirname, "public"),
        filename = path.join(publicDir, req.url);
    fs.readFile(filename, (err, data) => {
        if (err) {
            res.end("not found 404");
        } else {
            res.setHeader("Content-Type", mime.getType(filename));

            res.end(data);
        }
    });
}).listen(5000, () => {
    console.log("http://localhost:5000");
});